package com.example.bgmachine.bgmachine.orders;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.goods.HomepageGridRightMenuItemData;
import com.example.bgmachine.bgmachine.homes.HomePages;

import java.util.ArrayList;
import java.util.LinkedList;

public class OrderContentAdapter extends BaseAdapter {
    private Context mContext;
    private LinkedList<OrderContentData> mData = new LinkedList<OrderContentData>();

    public OrderContentAdapter(Context context, LinkedList<OrderContentData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.frament_order_list_item_content, null);
            convertViewtms= convertView;

            holder = new ViewHolder();
            holder.mOrderContentData = mData.get(position);
            holder.order_infor_one_textview = convertView.findViewById(R.id.order_infor_one_textview);
            holder.order_infor_two_textview = convertView.findViewById(R.id.order_infor_two_textview);
            holder.order_infor_time_textview = convertView.findViewById(R.id.order_infor_time_textview);

            holder.order_infor_one_textview.setText(holder.mOrderContentData.getmOrderNumber() +"-"+holder.mOrderContentData.getmContentName());
            holder.order_infor_two_textview.setText(holder.mOrderContentData.getSecondName()+"-"+holder.mOrderContentData.getCupType());
            holder.order_infor_time_textview.setText(holder.mOrderContentData.getmOrderTime());
            holder.frament_order_list_content_bottom_btn_operation_cancel  = convertView.findViewById(R.id.frament_order_list_content_bottom_btn_operation_cancel);
            holder.frament_order_list_content_bottom_btn_operation_cancel.setTag( holder.mOrderContentData );
            holder.frament_order_list_content_bottom_btn_operation_cancel.setEnabled(true);
            holder.frament_order_list_content_bottom_btn_operation_cancel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    v.setEnabled(false);
                    OrderContentData orderContentDatas =(OrderContentData)v.getTag(); // mData.get(ids);
                    if(orderContentDatas == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()del==cc=" );
                        return;
                    }

                    HomePages.mHomePages.deletOrderDetail(orderContentDatas);
                    v.setEnabled(true);
                }
            });

            holder.frament_order_list_content_bottom_btn_operation_make = convertView.findViewById(R.id.frament_order_list_content_bottom_btn_operation_make);
            holder.frament_order_list_content_bottom_btn_operation_make.setTag( holder.mOrderContentData );
            holder.frament_order_list_content_bottom_btn_operation_make.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    v.setEnabled(false);
                    OrderContentData orderContentDatas = (OrderContentData)v.getTag();

                    if(orderContentDatas == null){
                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=" );
                        return;
                    }

                    HomePages.mHomePages.makeOrderDetail( orderContentDatas );
                    v.setEnabled(true);
                }
            });
            holder.frament_order_list_content_bottom_btn_operation_make.setEnabled(true);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertViewtms.getTag();
            holder.mOrderContentData = mData.get(position);


            holder.frament_order_list_content_bottom_btn_operation_cancel = convertViewtms.findViewById(R.id.frament_order_list_content_bottom_btn_operation_cancel);
            holder.frament_order_list_content_bottom_btn_operation_cancel.setTag( holder.mOrderContentData );
            holder.frament_order_list_content_bottom_btn_operation_cancel.setEnabled(true);
            holder.frament_order_list_content_bottom_btn_operation_make = convertViewtms.findViewById(R.id.frament_order_list_content_bottom_btn_operation_make);
            holder.frament_order_list_content_bottom_btn_operation_make.setTag( holder.mOrderContentData );
            holder.frament_order_list_content_bottom_btn_operation_make.setEnabled(true);

            holder.order_infor_one_textview = convertViewtms.findViewById(R.id.order_infor_one_textview);
            holder.order_infor_two_textview = convertViewtms.findViewById(R.id.order_infor_two_textview);
            holder.order_infor_time_textview = convertViewtms.findViewById(R.id.order_infor_time_textview);


            holder.order_infor_one_textview.setText(holder.mOrderContentData.getmOrderNumber()+"-"+holder.mOrderContentData.getmContentName());
            holder.order_infor_two_textview.setText(holder.mOrderContentData.getSecondName()+"-"+holder.mOrderContentData.getCupType());
            holder.order_infor_time_textview.setText(holder.mOrderContentData.getmOrderTime());

        }

        return convertViewtms;
    }
    //添加一个元素
    public void add(OrderContentData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, OrderContentData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(OrderContentData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {
        public TextView order_infor_one_textview =null;
        public TextView order_infor_two_textview =null;
        public TextView order_infor_time_textview =null;

        public Button   frament_order_list_content_bottom_btn_operation_cancel = null;
        public Button   frament_order_list_content_bottom_btn_operation_make = null;


        public OrderContentData mOrderContentData = null;
    }


}
